package com.ebt.m.customer.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import com.ebt.m.R;

public class HalfWhiteBgTextView extends View{

	private int colorText = 0xff666666;
	/**
	 * 背景颜色主色调绿色
	 */
	private int colorA = 0xff2ca99f;
	/**
	 * 背景颜色白色
	 */
	private int colorB = 0xffffffff;
	
	private String mText = "正在计算FYP数据，请稍等...";
	
	private static final int DEFAULT_TEXT_SIZE_SP = 16;
	private int mTextSize = DEFAULT_TEXT_SIZE_SP;
	
	private Path path = new Path();
	private Paint paintText = new Paint();
	private Paint paintBg = new Paint();
	
	private Point pointL = new Point();
	private Point pointR = new Point();
	
	private RectF rectF = new RectF();
	
	private float textCenterYOffset;

	public HalfWhiteBgTextView(Context context) {
        super(context);
        init();
    }
    public HalfWhiteBgTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttr(context, attrs);
        init();
    }
    public HalfWhiteBgTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttr(context, attrs);
        init();
    }
    
    private void initAttr(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HalfWhiteBgTextView);

        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.HalfWhiteBgTextView_hwtv_TextSize:
                	mTextSize = a.getDimensionPixelSize(attr, DEFAULT_TEXT_SIZE_SP);
                    break;
                case R.styleable.HalfWhiteBgTextView_hwtv_Text:
                	mText = a.getString(attr);
                    break;
            }
        }
        a.recycle();
    }
    
    private void init(){
        colorA = getContext().getResources().getColor(R.color.customer_green_dark);
    	paintText.setAntiAlias(true);
    	paintText.setColor(colorText);
    	paintText.setTextSize(mTextSize);
    	paintText.setTextAlign(Align.CENTER);
    	textCenterYOffset = getTextCenterYOffset(paintText.getFontMetrics());
    	
    	paintBg.setAntiAlias(true);
    }
    
    private float getTextCenterYOffset(Paint.FontMetrics fontMetrics){
    	if(fontMetrics == null){
    		return 0;
    	}
    	return Math.abs(fontMetrics.top + fontMetrics.bottom)/2;
    }
    
    private int width;
    private int height;
    private int centerX;
    private int centerY;
    
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
        centerX = (width - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();
        centerY = (height - getPaddingBottom() - getPaddingTop()) / 2 + getPaddingTop();
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
    	super.onDraw(canvas);
    	
    	int textWidth = getTextWidth(mText);
    	int textHeight = getTextHeight(mText);
    	if(textWidth <= 0 || textHeight <= 0){
    		return;
    	}
    	canvas.drawColor(colorB);
    	int radius = textHeight;
    	
    	path.reset();
    	path.moveTo(getPaddingLeft(), getPaddingTop());
    	path.lineTo(width - getPaddingRight(), getPaddingTop());
    	path.lineTo(width - getPaddingRight(), centerY);
    	
    	int gap = (width - (textWidth + 3 * radius))/2;
    	gap = gap > 0 ? gap : 0;
    	pointL.x = getPaddingLeft() + gap;
    	pointL.y = centerY;
    	pointR.x = width - getPaddingRight() - gap;
    	pointR.y = centerY;
    	rectF.left = pointR.x - 2 * radius;
    	rectF.top = pointR.y - radius;
    	rectF.right = pointR.x;
    	rectF.bottom = pointR.y + radius;
    	
    	path.arcTo(rectF, 0, -90);
    	
    	path.lineTo(pointL.x + radius, pointL.y - radius);
    	rectF.offsetTo(pointL.x, pointL.y - radius);
    	path.arcTo(rectF, -90, -90);
    	path.lineTo(getPaddingLeft(), centerY);
    	path.close();
    	
    	paintBg.setColor(colorA);
    	canvas.drawPath(path, paintBg);
    	
    	canvas.drawText(mText, centerX, centerY + textCenterYOffset, paintText);
    }
    
    public void setText(String text){
    	this.mText = text;
    	invalidate();
    }
    
    private int getTextWidth(String text){
    	if(!TextUtils.isEmpty(text)){
    		return (int)(paintText.measureText(text) + 0.5f);
  		}
    	return -1;
    }
    
    private Rect textBounds = new Rect();
    private int getTextHeight(String text){
    	if(!TextUtils.isEmpty(text)){
    		paintText.getTextBounds(text, 0, text.length(), textBounds);
  			return textBounds.height();
  		}
    	return -1;
    }
}
